package abg.manage.controller.jy;

import abg.core.common.security.Auth;
import abg.core.common.util.StringKit;
import abg.core.common.util.valid.Ids;
import abg.core.common.util.valid.NotBlank;
import abg.core.common.util.valid.base.Valid;
import abg.core.domain.db.AaZip;
import abg.core.domain.search.ZipPsh;
import abg.core.domain.sys.MSG;
import abg.core.domain.sys.SRS;
import abg.core.domain.sys.ServiceException;
import abg.core.service.jldz.JldzService;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * 文件处理
 */
@RestController
@RequestMapping("/manage/jldz")
public class ZipC {
    @Resource
    private JldzService jldzService;


    @Auth(role = {"60"})
    @RequestMapping("/jwDeleteZipFile")
    public SRS<Object> jwDeleteZipFile(String ids) throws Exception {
        jldzService.jwDeleteZipFile(ids);
        return SRS.bySuccess();
    }

    @Auth(role = {"50"})
    @RequestMapping("/bmDeleteZipFile")
    public SRS<Object> bmDeleteZipFile(String ids) throws Exception {
        jldzService.bmDeleteZipFile(ids);
        return SRS.bySuccess();
    }

    /**
     * 部门：生成教师课程Zip包
     */
    @Valid
    @Auth(role = {"50"})
    @RequestMapping("/bmCreateJsZip")
    public SRS<Object> bmCreateJsZip(@NotBlank String zipName, @Ids String cjNumbers) throws Exception {
        //教师文件 0
        String zipType = "0";
        String bmCode = jldzService.getBmCode();
        boolean jsZip = jldzService.createJsZip(bmCode, "0", zipType, zipName, StringKit.split(cjNumbers));
        return SRS.bySuccess(jsZip);
    }

    /**
     * 教务处：生成教师课程Zip包
     */
    @Auth(role = {"60"})
    @RequestMapping("/jwCreateJsZip")
    public SRS<Object> jwCreateJsZip(@NotBlank String zipName, @Ids String cjNumbers) throws Exception {
        //教师文件 0
        String zipType = "0";
        //教务处
        boolean jsZip = jldzService.createJsZip(null, "1", zipType, zipName, StringKit.split(cjNumbers));
        return SRS.bySuccess(jsZip);
    }


    /**
     * 教务处：生成任务压缩包
     */
    @Auth(role = {"60"})
    @RequestMapping("/jwCreateTaskZip")
    public SRS<Object> jwCreateTaskZip(String zipName, String taskType, String taskIds) throws Exception {
        //教务处任务 1
        String zipType = "1";
        ArrayList<String> taskIdList = StringKit.split(taskIds);
        if (taskIdList.size() == 0) {
            throw new ServiceException(MSG.PARAM_ER);
        }
        jldzService.createTaskZip(zipType, "1", zipName, taskType, taskIdList);
        return SRS.bySuccess();
    }


    /**
     * 部门：当前部门教师压缩文件
     */
    @Auth(role = {"50"})
    @RequestMapping("/bmJsPageZip")
    public SRS<Page<AaZip>> bmJsPageZip(ZipPsh pageSearch) throws Exception {
        pageSearch.setZipType("0");//教师文件
        pageSearch.setBmCode(jldzService.getBmCode());
        pageSearch.setCreateUserType("0");//生成人身份；部门0；教务处1；
        Page page = jldzService.pageZip(pageSearch);
        return SRS.bySuccess(page);
    }

    /**
     * 教务处：所有教师压缩文件
     */
    @Auth(role = {"60"})
    @RequestMapping("/jwJsPageZip")
    public SRS<Page<AaZip>> jwJsPageZip(ZipPsh pageSearch) throws Exception {
        pageSearch.setZipType("0");//教师文件
        pageSearch.setCreateUserType("1");//生成人身份；部门0；教务处1；
        Page page = jldzService.pageZip(pageSearch);
        return SRS.bySuccess(page);
    }


    /**
     * 教务处：任务压缩文件分页
     */
    @Auth(role = {"60"})
    @RequestMapping("/jwTaskPageZip")
    public SRS<Page<AaZip>> jwTaskPageZip(ZipPsh pageSearch) throws Exception {
        pageSearch.setZipType("1");//教务处任务
        Page page = jldzService.pageZip(pageSearch);
        return SRS.bySuccess(page);
    }


}
